package com.gomyb.oss;

import com.aliyun.oss.OSSClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 2019/7/3 22:13
 */
@Slf4j
public class AliOSS extends FileServiceAbstract {

    @Override
    public String upload(MultipartFile file) {
        String path = getFilePath(file);
        try {
            OSSClient ossClient = new OSSClient(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());
            ossClient.putObject(config.getBucketName(), path, new ByteArrayInputStream(file.getBytes()));
            ossClient.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("oss上传失败", e);
        }
        return path;
    }
}
